package com.rain.base.service;

import com.rain.base.dao.LabelDao;
import com.rain.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import utils.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 标签的业务类
 * @auther 德哲
 * @date 2019/4/26 15:43.
 */
@Service
public class LabelService {

    @Autowired
    private LabelDao labelDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询所有标签
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 根据id去查询
     * @param id
     * @return
     */
    public Label findById(String id) {
        return labelDao.findById(id).get();
    }

    /**
     * 保存
     * @param label
     */
    public void save(Label label){
        //设置id
        label.setId(String.valueOf(idWorker.nextId()));
        labelDao.save(label);
    }

    /**
     * 修改
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        labelDao.deleteById(id);
    }

    /**
     * 根据条件查询
     * @param label
     * @return
     */
    public List<Label> findSearch(Label label){

        Specification<Label> spec = getLabelSpecification(label);

        return labelDao.findAll(spec);
    }

    /**
     * 根据条件查询
     * @param label
     * @return
     */
    public Page<Label> findSearch(Label label, int page, int size){

        Specification<Label> spec = getLabelSpecification(label);

        PageRequest pageRequest = PageRequest.of(page-1,size);

        return labelDao.findAll(spec,pageRequest);
    }

    /**
     * 查询条件
     * @param label
     * @return
     */
    private Specification<Label> getLabelSpecification(Label label) {
        return new Specification<Label>() {

            /**
             * 配置查询条件
             * @param root
             * @param criteriaQuery
             * @param criteriaBuilder
             * @return
             *
             * 条件：标签名称模糊查询  状态的精确查询   是否推荐的精确查询
             */
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                ArrayList<Predicate> predicates = new ArrayList<>();

                if (!StringUtils.isEmpty(label.getLabelname())) {
                    Predicate labelname = criteriaBuilder.like(root.get("labelname"), "%" + label.getLabelname() + "%");
                    predicates.add(labelname);
                }

                if (!StringUtils.isEmpty(label.getState())) {
                    Predicate state = criteriaBuilder.equal(root.get("state"), label.getState());
                    predicates.add(state);
                }

                if (!StringUtils.isEmpty(label.getRecommend())) {
                    Predicate recommend = criteriaBuilder.equal(root.get("recommend"), label.getRecommend());
                    predicates.add(recommend);
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
    }
}
